package uit.nfcmenu.view;

import java.util.ArrayList;

import uit.nfcmenu.db.model.ItemModel;
import uit.nfcmenu.util.ImageHelper;
import uit.nfcmenu.util.ImageLoader;
import uit.nfcmenu.util.UiUtilities.ICallBack;
import uit.nfcmenu.webservice.JSONParser;
import uit.nfcmenu.webservice.PreferenceHelper;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import uit.nfcmenu.R;

public class ItemAdapter extends BaseAdapter {
	String tag = ItemAdapter.class.getSimpleName();

	Context mContext;
	ArrayList<ItemModel> mModels;
	ICallBack mICallBack;
	String mLanguage;
	ImageLoader mImageLoader;
	
	// values for view
	int imgW = 0;
	int imgH = 0;
	int cornerRadius = 0;

	public ItemAdapter(Context context, ArrayList<ItemModel> models,
			ICallBack iCallBack) {
		mContext = context;
		mModels = models;
		mICallBack = iCallBack;
		mLanguage = PreferenceHelper.loadLanguage(mContext);
		mImageLoader = new ImageLoader(mContext, false);
		
		// assign values for view
		imgW = (int) (mContext.getResources().getDimension(R.dimen.item_width));
		imgH = (int) mContext.getResources().getDimension(R.dimen.item_height);
		cornerRadius = (int) mContext.getResources().getDimension(R.dimen.item_corner);
		
		// data for test
//		mModels.get(0).setItemName("Bánh mì thịt kẹp pho mát");
//		mModels.get(1).setItemName("Mực chiên");
//		mModels.get(2).setItemName("Cá, Khoai tây chiên và đậu Hà Lan");
//		mModels.get(3).setItemName("Mực chiên bột");
//		mModels.get(4).setItemName("Mực non chiên");
	}

	public void updateModels(ArrayList<ItemModel> models) {
		mModels = models;
	}
	
	@Override
	public int getCount() {
		return (mModels == null) ? 0 : mModels.size();
	}

	@Override
	public Object getItem(int position) {
		return (mModels == null || (position < 0 && position >= mModels.size())) 
				? null : mModels.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// init view
		ViewHolder holder;
		if (convertView == null) {
			final LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.inflater_item, null);
			holder = new ViewHolder();
			holder.bgImage = (ImageView)convertView.findViewById(R.id.img_detail_menu);
			holder.btnEvent = (Button) convertView.findViewById(R.id.img_event);
			holder.tvName = (TextView) convertView.findViewById(R.id.text_in_image);
			//Log.w(tag, "btnImage w="+holder.btnImage.getWidth()
			//+", h="+holder.btnImage.getHeight()); => 0,0
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		// init data
		ItemModel model = mModels.get(position);
		if (model != null) {
			String name = JSONParser.getStringByLanguage(model.getName(), mLanguage);
			holder.tvName.setText(name);
			holder.btnEvent.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					mICallBack.callBack(position);
				}
			});
			
//			mImageLoader.DisplayImage(mModels.get(position).getImageLink(), holder.btnImage, R.drawable.amablogo);
//			WaiterImageStoreManager.updateImage(mContext, holder.btnImage, model);
			
			// show image in ImageView obj
			//Log.i(tag, "setViewWithRoundCorners(): iv w="+imgW+", h="+imgH);
//			ImageHelper.setViewWithRoundCorners(holder.bgImage, model.getBitmap(), 
//					imgW, imgH, cornerRadius);
			ImageHelper.setBackground(holder.bgImage, model.getBitmap(), imgW, imgH);
		}
		return convertView;
	}

	class ViewHolder {
		ImageView bgImage;
		Button btnEvent;
		TextView tvName;
	}
}
